<template>
  <div class="contentForm">
    <el-form ref="form" :model="form" label-width="80px" size="mini" :rules="rules">
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="form.typeName" clearable :disabled="isDisabled" @change="typeNameChange" />
      </el-form-item>
      <el-form-item label="类型描述">
        <el-input v-model="form.typeDescribe" type="textarea" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item label="是否考核">
        <el-switch v-model="form.isAssess" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item label="启用">
        <el-switch v-model="form.isValid" :disabled="isDisabled" />
      </el-form-item>
      <!-- <el-form-item label="排序" prop="sort">
        <el-input-number v-model="form.sort" :step="1" :min="1" :max="max" @change="changeSortFun"></el-input-number>
      </el-form-item>-->
    </el-form>
  </div>
</template>

<script>
import { getByIdTaskType } from '@/api/portalTaskUi/taskType'
import { getTreeData } from '@/util/util'
export default {
  name: 'RightContentForm',
  inject: ['taskType'],
  data() {
    return {
      form: this.createInfo(),
      rules: {
        typeName: [
          { required: true, message: '请输入类型名称', trigger: ['blur', 'change'] }
        ]
        // sort: [
        //   { required: true, message: "请填写排序", trigger: "blur" },
        //   { type: "number", message: "排序必须为数字值" },
        // ],
      },
      row: {}
    }
  },
  computed: {
    isDisabled() {
      return Object.keys(this.row).length <= 0
    },
    max() { // 计算步长的最大值
      const tableData = this.taskType.getLeftTreeData()
      const currentId = this.row.pid
      if (currentId) {
        const targetData = this.row.pid === '0' ? tableData : getTreeData(tableData, currentId).children
        return targetData.length
      }
      return 100
    }
  },
  methods: {
    changeSortFun(val) { // 改变排序效果
      console.log(val)
      console.log(this.row)
    },
    typeNameChange(val) { // 改变类型名称
      if (!this.row.nodeId) {
        return
      }
      this.$set(this.row, 'nodeName', val)
    },
    createInfo() {
      return {
        typeName: '', // 类型名称
        typeDescribe: '', // 类型描述
        isAssess: false, // 是否考核
        isValid: false, // 是否启用
        isConfig: '1',
        sort: '' // 排序
      }
    },
    setData(val, currentId) {
      if (!val) {
        return
      }
      this.row = val
      if (val.isAdd || !val.nodeId) {
        this.form = Object.assign(this.createInfo(), { typeName: val.nodeName })
      } else {
        val.nodeId && this.getByIdTaskTypeFun(val.nodeId, currentId)
      }
    },
    getByIdTaskTypeFun(nodeId, currentId) {
      console.log(currentId)
      getByIdTaskType({ id: nodeId, tenantId: currentId }).then(res => {
        if (res.data.code === 0) {
          const data = res.data.data
          if (data === null) {
            this.form = this.createInfo()
          } else {
            const targetArr = ['isAssess', 'isValid']
            for (const key in this.form) {
              if (targetArr.includes(key)) {
                data[key] = data[key] === '1'
              }
              this.$set(this.form, key, data[key])
            }
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.contentForm {
  width: 100%;
  // height: 300px;
}
</style>
